package com.cl.blog.web.manager.examine;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cl.blog.common.Result;
import com.cl.blog.entity.BCommunity;
import com.cl.blog.entity.BUser;
import com.cl.blog.service.BCommunityService;
import com.cl.blog.service.BNewNoticeService;
import com.cl.blog.service.BUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author 付
 * @Date 2024-04-06 16:35
 * @Version 1.0
 * @description 社区审核
 * @since 1.0
 */

@RestController
@RequestMapping("/admin/community")
public class AdminCommunityController {

	@Autowired
	BCommunityService bCommunityService;
	@Autowired
	BUserService bUserService;
	@Autowired
	BNewNoticeService bNewNoticeService;

	@GetMapping("/list")
	public Result list(Integer currentPage, Integer status) {
		// 获取所有的评论列表
		Page<BCommunity> page = bCommunityService.page(new Page<>(currentPage, 20), new QueryWrapper<BCommunity>()
				.eq(status != 3, "status", status)
		);

		for (BCommunity record : page.getRecords()) {
			BUser bUser = bUserService.getById(record.getUserId());
			if (bUser != null) {
				record.setBUser(bUser);
			}
		}
		return Result.success(page);
	}


	// 重审社区问答
	@GetMapping("/reexamine")
	@Transactional(rollbackFor = Exception.class)
	public Result reExamine(Integer id, Integer userId, HttpServletRequest request) {
		BCommunity bCommunity = bCommunityService.getById(id);
		Assert.notNull(bCommunity, "抱歉，该数据不存在");
		Assert.isTrue(bCommunity.getStatus() != 0, "抱歉，还没有审核的数据无法进行重审");

		bCommunityService.update(new UpdateWrapper<BCommunity>()
				.eq("id", id)
				.set("status", 0)
		);
		bNewNoticeService.addNotice(userId, request);
		return Result.success();
	}

	// 删除社区问答
	@GetMapping("/delete")
	public Result delete(Integer id,HttpServletRequest request) {
		BCommunity bCommunity = bCommunityService.getById(id);
		Assert.notNull(bCommunity, "抱歉，该数据不存在");
		BCommunity byId = bCommunityService.getById(id);
		boolean id1 = bCommunityService.remove(new QueryWrapper<BCommunity>().eq("id", id));
		bNewNoticeService.communityExamine(request,byId.getUserId());
		return Result.success();
	}

	// 审核通过
	@GetMapping("/pass")
	@Transactional(rollbackFor = Exception.class)
	public Result pass(Integer id, Integer userId, HttpServletRequest request) {
		BCommunity bCommunity = bCommunityService.getById(id);
		Assert.notNull(bCommunity, "抱歉，该数据不存在");

		boolean id1 = bCommunityService.update(new UpdateWrapper<BCommunity>()
				.eq("id", id)
				.set("status", 1)
		);
		bNewNoticeService.passExamine(request, "community", id,userId);
		return Result.success();
	}

	// 审核未通过
	@GetMapping("/nopass")
	@Transactional(rollbackFor = Exception.class)
	public Result nopass(Integer id, Integer userId, HttpServletRequest request) {
		BCommunity bCommunity = bCommunityService.getById(id);
		Assert.notNull(bCommunity, "抱歉，该数据不存在");

		boolean id1 = bCommunityService.update(new UpdateWrapper<BCommunity>()
				.eq("id", id)
				.set("status", 2)
		);
		bNewNoticeService.noPassExamine(request, "community", id,userId);
		return Result.success();
	}

}
